package com.doit.demo04;

import java.io.*;

public class test_objectStream {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
//        writeObj();
        readObj();
    }

    //反序列化流
    private static void readObj() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day21\\obj\\person.txt"));
        //读取的时候,不会走person类,它只是把序列化文件读取      person.txt必须是序列化文件,不然读取不出来数据
        Object obj = ois.readObject();
        System.out.println(obj);
        ois.close();
    }

    //序列化流
    private static void writeObj() throws IOException {
        //创建序列化流对象
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day21\\obj\\person.txt"));
        Person p = new Person("柳岩", 38);
        oos.writeObject(p);
        oos.close();

//        new ObjectOutputStream(new FileOutputStream("day21\\obj\\person.txt"))
    }
}
